﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WordAutoGre
{
    public delegate void SaveProfile();
    public partial class ChoseFile : UserControl
    {
        public ChoseFile()
        {
            InitializeComponent();
        }
        public event SaveProfile save;
        protected virtual void OnSaveProfile()
        {
            if (save != null)
                save();
        }
        public string MyText
        {
            get
            {
                try
                {
                    FileInfo fi = new FileInfo(textBox1.Text);
                    return fi.FullName;
                }
                catch (Exception)
                {
                    return "";
                }

            }
            set
            {
                textBox1.Text = value;
            }
        }
        public bool IsValid()
        {
            string FileName = textBox1.Text;
            if (FileName.ToLower().EndsWith(".doc") && File.Exists(FileName))
            {
                textBox1.BackColor = System.Drawing.SystemColors.Window;
                return true;
            }
            textBox1.BackColor = System.Drawing.Color.PeachPuff; ;
            return false;
        }
        private void textBox1_DragDrop(object sender, DragEventArgs e)
        {
            TextBox textBox3 = (TextBox)sender;
            textBox3.Text = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            textBox3.SelectionStart = textBox3.Text.Length;
            OnSaveProfile();
        }

        private void textBox1_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.All;
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
  
        }

        private void textBox1_DoubleClick(object sender, EventArgs e)
        {
            TextBox tb = (TextBox)sender;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Title = "选择 WORD 文件";
            dialog.Filter = "WORD文件(*.doc)|*.doc|所有文件(*.*)|*.*";
            if (Directory.Exists(tb.Text))
            {
                dialog.InitialDirectory = tb.Text;
            }
            else if (File.Exists(tb.Text) && Directory.Exists(Path.GetDirectoryName(tb.Text)))
            {
                dialog.InitialDirectory = Path.GetDirectoryName(tb.Text);
            }
            else
            {
                dialog.InitialDirectory = Application.StartupPath;
            }
            dialog.ShowDialog();
            if (File.Exists(dialog.FileName))
            {
                tb.Text = dialog.FileName;
                OnSaveProfile();
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if(IsValid())
            OnSaveProfile();
        }

        private void ChoseFile_Load(object sender, EventArgs e)
        {
            IsValid();
        }
        ToolTip callinfo = new ToolTip();
        private void textBox1_MouseEnter(object sender, EventArgs e)
        {
            if (textBox1.Text.Trim() != "" && textBox1.Text.Length > 30)
            {
                callinfo.SetToolTip(textBox1, textBox1.Text);
            }
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {
            if (IsValid())
            {
                Form1.OpenWord(textBox1.Text);
            }
        }

    }
}
